package boot.controller;


import boot.domain.Product;
import boot.domain.Supplier;
import boot.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author enthalpy
 * @since 2022-10-28
 */
@RestController
@RequestMapping("/suppliers")
public class SupplierController {
    final
    SupplierServiceImpl supplierService;

    public SupplierController(SupplierServiceImpl supplierService) {
        this.supplierService = supplierService;
    }

    @GetMapping
    @PreAuthorize("hasAuthority('query')")
    public List<Supplier> getAll(){
        return supplierService.list();
    }

    @PostMapping
    @PreAuthorize("hasAuthority('update')")
    public boolean save(@RequestBody Supplier supplier){
        return supplierService.save(supplier);
    }

    @PutMapping
    @PreAuthorize("hasAuthority('update')")
    public boolean update(@RequestBody Supplier supplier){
        return supplierService.update(supplier, null);
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasAuthority('update')")
    public boolean delete(@PathVariable int id){
        return supplierService.removeById(id);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('query')")
    public Supplier getById(@PathVariable int id){
        return supplierService.getById(id);
    }
}

